package com.czwj.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.ForumPost;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.ForumPostService;
import com.czwj.util.TextUtil;

/**
* @ClassName: ForumPostAction
* @Description: TODO
* @author H
* @date 2020年6月1日
*
*/
@Controller
public class ForumPostAction{
	@Resource
	ForumPostService forumPostService;
	
	@RequestMapping(value = "/forumPosts",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ForumPost> forumPost(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "hightlight",required = false) String hightlight,
			@RequestParam(value = "name",required = false) String name,
			@RequestParam(value = "title",required = false) String title,
			@RequestParam(value = "flag",required = false) Boolean flag){
		DataGridData<ForumPost> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		if(TextUtil.isNotEmpty(hightlight)){
			map.put("hightlight",hightlight);
		}
		if(TextUtil.isNotEmpty(name)){
			map.put("name",name);
		}
		if(TextUtil.isNotEmpty(title)){
			map.put("title",title);
		}
		if(flag != null && flag == true){
			Subject currentUser = SecurityUtils.getSubject();
			name = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getName();
			map.put("name",name);
		}
		List<ForumPost> list = forumPostService.search(map);
		result.setRows(list);
		result.setTotal(forumPostService.count(map));
		return result;
	}
	
	@SystemControllerLog(description = "发帖")
	@RequestMapping(value = "/forumPostSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult forumPostSave(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "title",required = false) String title,
			@RequestParam(value = "content",required = false) String content,HttpServletRequest request){
		OperateResult result = new OperateResult();
		int count = 0;
		Subject currentUser = SecurityUtils.getSubject();
		int createrid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		String createip = request.getRemoteAddr();
		count = forumPostService.insertorupdate(id,title,content,createrid,createip,images);
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删帖")
	@RequestMapping(value = "forumPostDelete.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult forumPostDelete(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = forumPostService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "加精")
	@RequestMapping(value = "/forumPostHighlight.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult forumPostUpdate(@RequestParam(value = "id",required = false) Integer id){
		OperateResult result = new OperateResult();
		ForumPost forumPost = forumPostService.selectByPrimaryKey(id);
		forumPost.setHightlight(2);
		int count = forumPostService.update(forumPost);
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "审核帖子")
	@RequestMapping(value = "/forumPostReview.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult forumPostReview(@RequestParam(value = "id",required = false) Integer id){
		OperateResult result = new OperateResult();
		ForumPost forumPost = forumPostService.selectByPrimaryKey(id);
		forumPost.setStatus(2);
		int count = forumPostService.update(forumPost);
		result.setSuccess(count > 0);
		return result;
	}
}
